package com.hy;


import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.net.URI;
import java.util.List;
import java.util.Map;

/**
 * @author chenzeke
 */
@Slf4j
public class HttpSession {

    private HttpClientContext httpClientContext;
    public CookieStore cookieStore;

    public HttpSession() {
        CookieStore cookieStore = new BasicCookieStore();
        this.cookieStore = cookieStore;
        httpClientContext = HttpClientContext.create();
        httpClientContext.setCookieStore(cookieStore);
    }

    public CloseableHttpClient getHttpClient() {
        //不允许重定向
        RequestConfig config = RequestConfig.custom().setRedirectsEnabled(true).build();
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config).build();
        return httpClient;
    }

    /**
     * 发送get请求；核心方法
     */
    public HttpClientResult doGet(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
        CloseableHttpClient httpClient = getHttpClient();
        HttpGet httpGet = HttpClientUtils.doGetHandle(url, headers, params);
        return HttpClientUtils.getHttpClientResult(httpClient, httpGet, httpClientContext);
    }

    /**
     * 发送get请求；核心方法excute
     */
    public CloseableHttpResponse execute(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
        CloseableHttpClient httpClient = getHttpClient();
        HttpGet httpGet = HttpClientUtils.doGetHandle(url, headers, params);
        return httpClient.execute(httpGet);
    }


    /**
     * 发送Post请求；核心方法
     */
    public HttpClientResult doPost(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
        CloseableHttpClient httpClient = getHttpClient();
        HttpPost httpPost = HttpClientUtils.doPosthandle(url, headers, params);
        HttpClientResult resultContent = HttpClientUtils.getHttpClientResult(httpClient, httpPost, httpClientContext);
        return resultContent;
    }

    public CloseableHttpResponse doPost2(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
        HttpPost httpPost = HttpClientUtils.doPosthandle(url, headers, params);
        CloseableHttpClient httpClient = getHttpClient();
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpHost target = httpClientContext.getTargetHost();
        List<URI> redirectLocations = httpClientContext.getRedirectLocations();
        URI location = URIUtils.resolve(httpPost.getURI(), target, redirectLocations);
        String absUrl = location.toASCIIString();
        log.info("absUrl==>" + absUrl);
        return response;
    }

    /**
     * 发送Post请求；核心方法execute
     */
    public CloseableHttpResponse executePost(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
        CloseableHttpClient httpClient = getHttpClient();
        HttpPost httpPost = HttpClientUtils.doPosthandle(url, headers, params);
        return httpClient.execute(httpPost);
    }

    public HttpClientContext getHttpClientContext() {
        return httpClientContext;
    }
}
